package com.xxxx.crm.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xxxx.crm.base.BaseService;
import com.xxxx.crm.dao.DepartmentMapper;
import com.xxxx.crm.query.DepartmentQuery;
import com.xxxx.crm.utils.AssertUtil;
import com.xxxx.crm.utils.PhoneUtil;
import com.xxxx.crm.vo.Department;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class DepartmentService extends BaseService<Department,Integer> {
    @Resource
    private DepartmentMapper departmentMapper;

    //多条件分页查询(返回的数据格式必须满足LayUi的数据表格要求的格式)
    public Map<String , Object> queryDepartmentByParams(DepartmentQuery departmentQuery){
        Map<String , Object> map=new HashMap<>();

        //开启分页
        PageHelper.startPage(departmentQuery.getPage(),departmentQuery.getLimit());
        //得到对应的分页对象
        PageInfo<Department> pageInfo=new PageInfo<>(departmentMapper.selectByParams(departmentQuery));
        //设置map对象
        map.put("code",0);
        map.put("msg","sucdess");
        map.put("count",pageInfo.getTotal());
        //设置分页好的列表
        map.put("data",pageInfo.getList());


        return map;
    }
    /*
        添加部门
        1.参数校验
            departmentName 非空
            linkMan 非空
            linkPhone 非空，格式正确
        2.设置相关参数的默认值
            isValid 默认有效 1=有效
            createDate创建时间默认系统当前时间
        3.执行添加操作，判断受影响的行数
     */
    @Transactional(propagation =  Propagation.REQUIRED)
    public void addDepartment(Department department){
        /*校验参数*/

        checkDepartmentParams(department.getDepartmentName(),department.getLinkMan(),department.getLinkPhone());

        //设置默认值
        department.setIsValid(1);
        department.setCreateDate(new Date());
        //执行添加操作
        AssertUtil.isTrue(departmentMapper.insertSelective(department)!=1,"添加部门失败！");
    }

    /*更新部门
        1.参数校验
            部门ID 非空，数据库中对应的记录存在
            departmentName 非空
            linkMan 非空
            linkPhone 非空，格式正确

        2.设置相关参数的默认值
            不需要重新设置默认值

        3.执行更新操作，判断受影响行数
     */

    @Transactional(propagation =  Propagation.REQUIRED)
    public void updateDepartment(Department department){
        //1.参数校验
        AssertUtil.isTrue(null==department.getId(),"待更新记录不存在！");
        //通过主键查询对象
        Department temp = departmentMapper.selectByPrimaryKey(department.getId());
        //判断数据库中对应的记录存在
        AssertUtil.isTrue(temp==null,"待更新记录不存在");
        //参数校验
        checkDepartmentParams(department.getDepartmentName(),department.getLinkMan(),department.getLinkPhone());
        //3.执行更新操作，判断受影响的行数
        AssertUtil.isTrue(departmentMapper.updateByPrimaryKeySelective(department)!=1,"更新部门失败");
    }

    //参数校验
    private void checkDepartmentParams(String departmentName, String linkMan, String linkPhone) {
        AssertUtil.isTrue(StringUtils.isBlank(departmentName),"部门名称不能为空！");
        AssertUtil.isTrue(StringUtils.isBlank(linkMan),"部门经理不能为空！");
        AssertUtil.isTrue(StringUtils.isBlank(linkPhone),"部门电话不能为空！");
        AssertUtil.isTrue(!PhoneUtil.isMobile(linkPhone),"部门电话格式错误！");

    }

    /*
     * 删除部门
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public void deleteDepartment(Integer[] ids) {
        // 判断ID是否为空
        AssertUtil.isTrue(null == ids || ids.length < 1, "待删除记录不存在！");
        // 执行删除（更新）操作，判断受影响的行数
        AssertUtil.isTrue(departmentMapper.deleteBatch(ids) != ids.length, "部门数据删除失败！");
    }

}
